package com.cash.loan.modular.cash.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cash.loan.modular.cash.dao.ProductAdCateMapper;
import com.cash.loan.modular.cash.dao.ProductAdPictureMapper;
import com.cash.loan.modular.cash.model.ProductAdCate;
import com.cash.loan.modular.cash.service.AdCateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Administrator
 * @description
 * @date 2018年09月30 19:53
 */
@Service
public class AdCateServiceImpl implements AdCateService {

    private static final Logger log = LoggerFactory.getLogger(AdCateServiceImpl.class);

    @Autowired
    private ProductAdCateMapper adCateMapper;

    @Autowired
    private ProductAdPictureMapper productAdPictureMapper;

    @Override
    public List<ProductAdCate> list() {
        List<ProductAdCate> productAdCateList = adCateMapper
                .selectList(new EntityWrapper<ProductAdCate>());
        return productAdCateList;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean add(String adCateName) {
        ProductAdCate productAdCate = new ProductAdCate();
        productAdCate.setName(adCateName);
        Integer addRes = adCateMapper.insert(productAdCate);
        if(addRes != 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public ProductAdCate detail(Integer id) {
        ProductAdCate productAdCate = adCateMapper.selectById(id);
        return productAdCate;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean update(String adCateName, Integer id) {
        ProductAdCate productAdCate = new ProductAdCate();
        productAdCate.setName(adCateName);
        productAdCate.setId(id);
        Integer updateRes = adCateMapper.updateById(productAdCate);
        if(updateRes != 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean delete(Integer id) {
        productAdPictureMapper.deleteByCateId(id);
        adCateMapper.deleteById(id);
        return true;
    }
}
